package service

import (
	"blog-service/internal/model"
	"blog-service/pkg/app"
)

type CountTagRequest struct {
	Name  string `form:"name" binding:"max=100"`
	State uint8  `form:"state,default=1" binding:"oneof=0 1"`
}

type TagListRequest struct {
	Name  string `form:"name" binding:"max=100"`
	State uint8  `form:"state,default=1" binding:"oneof=0 1"`
}

type CreateTagRequest struct {
	Name      string `form:"name" binding:"required,min=3,max=100"`
	CreatedBy string `form:"created_by" binding:"required,min=3,max=100"`
	State     uint8  `form:"state,default=1" binding:"oneof=0 1"`
}

type UpdateTagRequest struct {
	ID         uint32 `form:"id" binding:"required,gte=1"`
	Name       string `form:"name" binding:"min=0,max=100"`
	State      uint8  `form:"state" binding:"oneof=0 1"`
	ModifiedBy string `form:"modified_by" binding:"min=3,max=100"`
}

type DeleteTagRequest struct {
	ID uint32 `form:"id" binding:"required,gte=1"`
}

func (svc *Service) CountTag(param *CountTagRequest) (int, error) {
	tagModel := model.Tag{Name: param.Name, State: param.State}
	return tagModel.CountTag()
}

func (svc *Service) GetTagList(param *TagListRequest, pager *app.Pager) ([]*model.Tag, error) {
	tagModel := model.Tag{Name: param.Name, State: param.State}
	pageOffset := app.GetPageOffset(pager.Page, pager.PageSize)
	return tagModel.GetTagList(pageOffset, pager.PageSize)
}

func (svc *Service) CreateTag(param *CreateTagRequest) error {
	tagModel := model.Tag{Name: param.Name, State: param.State, Model: &model.Model{CreatedBy: param.CreatedBy}}
	return tagModel.CreateTag()
}

func (svc *Service) UpdateTag(param *UpdateTagRequest) error {
	tagModel := model.Tag{
		Model: &model.Model{ID: param.ID},
	}
	values := map[string]interface{}{
		"state":       param.State,
		"modified_by": param.ModifiedBy,
	}
	if param.Name != "" {
		values["name"] = param.Name
	}
	return tagModel.UpdateTag(values)
}

func (svc *Service) DeleteTag(param *DeleteTagRequest) error {
	tagModel := model.Tag{Model: &model.Model{ID: param.ID}}
	return tagModel.DeleteTag()
}
